class AdminController < ApplicationController
  layout 'admin'
  skip_before_filter :check_db, :only => [:setup_db]
  skip_before_filter :check_configuration, :only => [:setup, :setup_post, :setup_db]
  
  # Plaseholder for main 
  def main
  end
  
  # This is the logic used to walk the user through the configuration of the system
  def setup
    
    # These are the wizard pages, and this determines the order of the steps
    steps = [:admin, :secure, :settings, :calendars, :feeds, :complete]
    complete = []
    
    @mode = {}
    steps.each do |step|
      @mode[step] = 'standard'
    end
    
    # Do the admin user checks
    if User.find_by_admin(1) != nil then    
      complete.push :admin
    end
    
    complete.push :secure
    complete.push :settings
    complete.push :calendars
    complete.push :feeds
    
    complete.each do |step|
      @mode[step] = 'complete'  
      steps.delete_at(steps.index(step))
    end
    
    if params[:mode] == nil
      @mode[steps.first] = 'selected'
      page = "setup_#{steps.first.to_s}".to_sym    
    else
      @mode[params[:mode].to_sym] = 'selected'
      page = "setup_#{params[:mode]}".to_sym    
    end
          
    render page
  end
  
  def setup_post
    if params[:mode] == 'admin'
      setup_admin(params[:user]);
    elsif params[:mode] == 'secure'      
    end
    
    setup
  end
  
  # Placeholder for view
  def setup_db
  end
  
  private
  def setup_admin(user)
    # Create the user object
    u = User.new(user)    

    # Set it as an admin account (this method is for this purpose)
    u.admin = 1
    if u.valid?  
      # TODO: Catch any exceptions raised by the save
      u.save
    else
      @setup_mode = :admin
      @user = u
      @errors = u.errors
    end
  end
  
 
end
